﻿using System.Linq;
using System.Web.Mvc;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.Interfaces;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.MSSQL;
using iss.sg4210.se18.team6s.Models;

namespace iss.sg4210.se18.team6s.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/SearchAll

        public ActionResult SearchAll(SearchModel searchModel, string searchButton)
        {
            ISearch search = new SearchDAO();

            IQueryable<Advertisement> searchResults = null;
            if (searchButton.Equals("Display All"))
                searchResults = search.GetAllSearchResults();
            if (Request.IsAjaxRequest())
                return View("SearchResults", searchResults);
            else
                return View(search.GetAllSearchResults());
        }

        //
        // GET: /Search/Search

        public ActionResult Search()
        {
            return View();
        }

        //
        // POST: /Search/Create

        [HttpPost]
        public ActionResult Search(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                SearchModel searchModel = new SearchModel();
                UpdateModel(searchModel, collection);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Details(int id)
        {
            Advertisement advertisement = DataAccessFactory.Instance.Advertisement.GetAdvertisement(id);
            return View(advertisement);
        }
    }
}
